<template>
  <div class="app-container">
    <div class="header flex">
      <FormSearch :formFields="searchList" @search="Search" @reset="Search" />
    </div>
    <div class="footer">
      <a-tabs
        v-model:activeKey="state.searchParams.order_status"
        @change="changeStatus"
      >
        <a-tab-pane
          :key="item.value"
          :tab="item.label"
          v-for="item in state.orderStatus"
        >
          <a-table
            :columns="columns"
            :data-source="state.data"
            rowKey="order_id"
            :pagination="state.pagination"
            @change="pageChange"
            :loading="state.loading"
            :scroll="{ x: 1200 }"
          >
            <template #bodyCell="{ record, column }">
              <!-- 商品名称 -->
              <template v-if="column.dataIndex === 'orderGoods'">
                <div class="flex gap">
                  <a-image :width="80" :src="record.orderGoods.goods_img" />
                  <div class="good-name">
                    <a-tooltip>
                      <template #title>{{
                        record.orderGoods.goods_name
                      }}</template>
                      {{ record.orderGoods.goods_name }}
                    </a-tooltip>
                  </div>
                </div>
              </template>
              <template v-if="column.dataIndex === 'actions'">
                <a-space size="small" align="baseline">
                  <a-button type="link" @click="clickCheck(record)"
                    >详情</a-button
                  >
                </a-space>
              </template>
            </template>
          </a-table>
        </a-tab-pane>
      </a-tabs>
      <DetailsDrawer ref="details" @modal-success="refresh" />
    </div>
  </div>
</template>
<script setup>
import { reactive, ref } from "vue";
import { list } from "@/api/order";
import dayjs from "dayjs";
import DetailsDrawer from "./DetailsDrawer.vue";

const details = ref();
const searchList = reactive([
  {
    type: "input",
    label: "订单编号",
    name: "order_no",
    value: "",
    placeholder: "请输入订单编号",
  },
  {
    type: "input",
    label: "支付单号",
    name: "pay_no",
    value: "",
    placeholder: "请输入支付单号",
  },
  {
    type: "input",
    label: "商品名称",
    name: "goods_name",
    value: "",
    placeholder: "请输入商品名称",
  },
  {
    type: "input",
    label: "收货人",
    name: "ship_name",
    value: "",
    placeholder: "请输入收货人",
  },
  {
    type: "input",
    label: "收货人手机号",
    name: "ship_phone",
    value: "",
    placeholder: "请输入收货人手机号",
  },
  {
    type: "input",
    label: "用户手机号",
    name: "user_phone",
    value: "",
    placeholder: "请输入用户手机号",
  },
  {
    type: "range-picker",
    label: "创建时间",
    name: "create_time",
    value: [],
    placeholder: "请选择",
    showTime: true,
  },
  {
    type: "select",
    label: "订单类型",
    name: "order_type",
    value: null,
    placeholder: "请选择订单类型",
    allowClear: true,
    options: [
      {
        id: 1,
        name: "普通",
      },
      {
        id: 2,
        name: "兑换",
      },
      {
        id: 3,
        name: "申购",
      },
      {
        id: 4,
        name: "特价",
      },
      {
        id: 5,
        name: "膨胀金",
      },
    ],
  },
  {
    type: "select",
    label: "提货类型",
    name: "shop_type",
    value: null,
    placeholder: "请选择提货类型",
    allowClear: true,
    options: [
      {
        id: 1,
        name: "快递",
      },
      {
        id: 2,
        name: "自提",
      },
    ],
  },
  {
    type: "select",
    label: "付款方式",
    name: "pay_type",
    value: null,
    placeholder: "请选择付款方式",
    allowClear: true,
    options: [
      {
        id: 1,
        name: "微信",
      },
      {
        id: 2,
        name: "支付宝",
      },
      {
        id: 3,
        name: "余额",
      },
    ],
  },
  {
    type: "select",
    label: "是否虚拟商品",
    name: "is_virtually",
    value: 0,
    placeholder: "请选择付款方式",
    allowClear: true,
    options: [
      {
        id: 0,
        name: "全部",
      },
      {
        id: 1,
        name: "虚拟商品",
      },
      {
        id: 2,
        name: "普通商品",
      },
    ],
  },
  {
    type: "switch",
    label: "提醒发货",
    name: "is_notice",
    value: null,
    checkedChildren: "是",
    unCheckedChildren: "否",
    checkedValue: 1,
    unCheckedValue: 0,
  },
]);
const columns = [
  {
    title: "id",
    dataIndex: "order_id",
    width: 100,
  },
  {
    title: "订单编号",
    dataIndex: "order_no",
    width: 180,
  },
  {
    title: "商品信息",
    dataIndex: "orderGoods",
    width: 300,
  },
  {
    title: "订单类型",
    dataIndex: "order_type_name",
    width: 150,
  },
  {
    title: "提货类型",
    dataIndex: "shop_type_name",
    width: 150,
  },
  {
    title: "订单金额",
    dataIndex: "goods_total",
    width: 150,
    customRender(record) {
      return "￥" + record.value || "-";
    },
  },
  {
    title: "订单状态",
    dataIndex: "order_status_name",
    width: 150,
  },
  {
    title: "创建时间",
    dataIndex: "create_time",
    width: 180,
  },
  {
    title: "操作",
    dataIndex: "actions",
    fixed: "right",
    width: 100,
  },
];
const state = reactive({
  orderStatus: [
    { value: "-1", label: "全部订单" },
    {
      value: "1",
      label: "待发货",
    },
    {
      value: "2",
      label: "待收货",
    },
    {
      value: "3",
      label: "已收货",
    },
    {
      value: "4",
      label: "已完成",
    },
    {
      value: "5",
      label: "已关闭",
    },
    {
      value: "6",
      label: "进行中",
    },
  ],
  data: [], //表格的数据
  loading: false,
  pagination: {
    total: 0,
    showSizeChanger: true, //是否页码可以改变
    current: 1, //当前xx页面
    pageSize: 10, //一页展示xx条数据
    showTotal: function (total) {
      return "总共" + total + "条数据";
    }, //总共xx条
    showQuickJumper: true,
  }, //分页数据
  searchParams: {
    order_status: "-1",
    page: 1,
    limit: 10,
  },
});
//分页变动数据
const pageChange = (pagination) => {
  state.pagination.current = pagination.current;
  state.pagination.pageSize = pagination.pageSize;
  //搜索的参数
  state.searchParams.page = pagination.current;
  state.searchParams.limit = pagination.pageSize;
  //搜索
  getList();
};

//获取列表的数据
const getList = () => {
  state.loading = true;
  list({
    ...state.searchParams,
  }).then((res) => {
    setTimeout(function () {
      state.loading = false;
    }, 500);
    if (res.code == 0) {
      state.data = res.data.data;
      state.pagination.total = res.data.total;
    }
  });
};
const changeStatus = (activeKey) => {
  state.pagination.current = 1;
  getList();
};

// 查看详情
const clickCheck = (record) => {
  details.value.show(record);
};
//搜索事件
const Search = (values) => {
  state.searchParams.page = 1;
  //合并之前的搜索参数
  state.searchParams = {
    ...state.searchParams,
    ...values,
    create_time:
      values.create_time.length > 0
        ? [
            dayjs(values.create_time[0]).format("YYYY-MM-DD HH:mm:ss"),
            dayjs(values.create_time[1]).format("YYYY-MM-DD HH:mm:ss"),
          ]
        : [],
  };
  //搜索
  getList();
};
const refresh = () => {
  getList();
};
getList();
</script>
<style lang="scss" scoped>
@import "./index";
</style>
  